{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 内建函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python内置函数教程： \n",
    "http://www.runoob.com/python/python-built-in-functions.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、 Range\n",
    "range(start, stop[, step])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]\n"
     ]
    }
   ],
   "source": [
    "# 生成以5开始，到99，间隔为5\n",
    "print(list(range(5,100,5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、 Len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回长度\n",
    "len(list(range(5,100,5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、 Map\n",
    "\n",
    "map(function, iterable, ...)\n",
    "\n",
    "方法： X**2\n",
    "\n",
    "输入： [1,2,3,4,5]\n",
    "\n",
    "返回： [1,4,9,16,25]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "print(list(map(lambda x: x**2, range(1,6))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法： $x + y$\n",
    "\n",
    "输入： [1,2,3,4,5] , [8,9,10,11,12]\n",
    "\n",
    "返回： [9, 11, 13, 15, 17]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 11, 13, 15, 17]\n"
     ]
    }
   ],
   "source": [
    "print(list(map(lambda x, y: x + y, range(1,6), range(8,13))))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4、 Reduce\n",
    "\n",
    "reduce(function, iterable[, initializer])\n",
    "\n",
    "reduce() 函数会对参数序列中元素进行累积。\n",
    "\n",
    "函数将一个数据集合（链表，元组等）中的所有数据进行下列操作：用传给reduce中的函数 function（有两个参数）先对集合中的第 1、2 个元素进行操作，得到的结果再与第三个数据用 function 函数运算，最后得到一个结果。\n",
    "\n",
    "reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "def add(x, y):\n",
    "    return x+ y\n",
    "\n",
    "print(reduce(add, [1, 3, 5, 7, 9]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35\n"
     ]
    }
   ],
   "source": [
    "print(reduce(add, [1, 3, 5, 7, 9], 10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5、 Filter\n",
    "Python内建的filter()函数用于过滤序列。\n",
    "\n",
    "和map()类似，filter()也接收一个函数和一个序列。和map()不同的是，filter()把传入的函数依次作用于每个元素，然后根据返回值是True还是False决定保留还是丢弃该元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 6, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "def is_odd(n):\n",
    "    return (n>=5) & (n<11)\n",
    "\n",
    "print(list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6、 Sorted\n",
    "\n",
    "sorted(iterable[, key[, reverse])\n",
    "\n",
    "iterable -- 可迭代对象。\n",
    "\n",
    "key -- 主要是用来进行比较的元素，只有一个参数，具体的函数的参数就是取自于可迭代对象中，指定可迭代对象中的一个元素来进行排序。\n",
    "\n",
    "reverse -- 排序规则，reverse = True 降序 ， reverse = False 升序（默认）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sorted?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[70, 92, 175, 1000]\n"
     ]
    }
   ],
   "source": [
    "print(sorted([92, 175, 70, 1000]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['MSFT', 'IBKR', 'GOOG', 'APPL']\n"
     ]
    }
   ],
   "source": [
    "print(sorted(['MSFT', 'APPL', 'IBKR', 'GOOG'],  reverse=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "L = [('MSFT', 92), ('APPL', 175), ('IBKR', 70), ('GOOG', 1000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('APPL', 175), ('GOOG', 1000), ('IBKR', 70), ('MSFT', 92)]\n"
     ]
    }
   ],
   "source": [
    "L2 = sorted(L, key=lambda x:x[0])\n",
    "print(L2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('GOOG', 1000), ('APPL', 175), ('MSFT', 92), ('IBKR', 70)]\n"
     ]
    }
   ],
   "source": [
    "L3 = sorted(L, key=lambda x:x[1], reverse=True)\n",
    "print(L3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
